package concurrent.application;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.xerces.jaxp.SAXParserImpl;
import org.apache.xerces.parsers.DOMParser;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.junit.Test;
import org.xml.sax.Parser;
import org.xml.sax.SAXException;

import javax.xml.parsers.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;



/**
 * @program JavaBase
 * @description:
 * @author: zhanglu
 * @create: 2021-06-01 15:39:00
 */
@Slf4j
public class SemaphoreDemo {

    @Test
    public void test01() throws InterruptedException {
        Semaphore semaphore = new Semaphore(0);
        ExecutorService service = Executors.newFixedThreadPool(2);
        for (int i = 0; i < 2; i++) {
            service.submit(() -> {
                log.info("over~");
                semaphore.release();
            });
        }
        semaphore.acquire(2);
        log.info("all over~");
        service.shutdown();
    }

    @Test
    public void test02() throws InterruptedException {

        Semaphore semaphore = new Semaphore(0);
        ExecutorService service = Executors.newFixedThreadPool(2);
        for (int i = 0; i < 2; i++) {
            service.submit(() -> {
                log.info("A start~");
                semaphore.release();
            });
        }
        semaphore.acquire(2);
        for (int i = 0; i < 2; i++) {
            service.submit(() -> {
                log.info("B start~");
                semaphore.release();
            });
        }
        semaphore.acquire(2);
        log.info("all over~");
        service.shutdown();
    }

//    ThreadLocal<String> s = new ThreadLocal<>();
    ThreadLocal<String> s = ThreadLocal.withInitial(() -> "1111");

    @Test
    public void test03() throws InterruptedException {
//        ExecutorService executorService = Executors.newFixedThreadPool(20);
//        for (int i = 0; i < 10000; i++) {
//            executorService.submit(() -> {
//                try {
//                    String id = IdUtil.fastSimpleUUID();
//                    s.set(id);
//                    TimeUnit.MILLISECONDS.sleep(10);
//                    log.info(s.get());
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            });
//        }
//        int count = 0;
//        while (true){
//            if(count > 20){
//                break;
//            }
//            Thread thread = Thread.currentThread();
//            s.get();
//            TimeUnit.SECONDS.sleep(1);
//            count ++;
//        }
//        Thread thread = Thread.currentThread();
//        s.get();
//        System.gc();
//        TimeUnit.SECONDS.sleep(4);
//        thread = Thread.currentThread();
//        s.get();

        Runnable r = () -> {
            try {
                String id = IdUtil.fastSimpleUUID();
                s.set(id);
                TimeUnit.MILLISECONDS.sleep(10);
                log.info(s.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                s.remove();
            }
        };
        Thread t1 = new Thread(r);
        Thread t2 = new Thread(r);
        t1.start();
        t2.start();
        System.gc();
        TimeUnit.SECONDS.sleep(4);
        Thread t3 = new Thread(r);
        Thread t4 = new Thread(r);
        t3.start();
        t4.start();
        TimeUnit.SECONDS.sleep(4);
        Thread.currentThread().join();
    }

    @Test
    public void test04(){
        System.out.println(s.get());
        s.set("111");
        System.out.println(s.get());
        s.set("222");
        System.out.println(s.get());
        T t = new T();
        t.set(s.get());
        System.out.println(t.get());
        s.remove();
        System.out.println(s.get());
        System.out.println(t.get());
        t.s.remove();
        System.out.println(t.get());
    }

    class T{
        public ThreadLocal<String> s  = new ThreadLocal<String>();

        public void set(String d){
            s.set(d);
        }

        public String get(){
            return s.get();
        }
    }

    @Test
    public void test011() {
        String res = null;
        try {
            String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<Root Type=\"1\">\n" +
                    "<StartSign>0x7EE7</StartSign>\n" +
                    "<TimeStamp(s)>1625166484</TimeStamp(s)>\n" +
                    "<TimeStamp(ms)>529</TimeStamp(ms)>\n" +
                    "<SeqNum>1390254</SeqNum>\n" +
                    "<IcpId>423242-29943068</IcpId>\n" +
                    "<SourceOMC>Huawei-IMS-VoLTE_IMS_U2000</SourceOMC>\n" +
                    "<Tradname>1</Tradname>\n" +
                    "<Vendor>007</Vendor>\n" +
                    "<Version></Version>\n" +
                    "<IcpProbeId>423242</IcpProbeId>\n" +
                    "<MsgFormat>1</MsgFormat>\n" +
                    "<Reserve></Reserve>\n" +
                    "<CollectType>1</CollectType>\n" +
                    "<LenOfBody>541</LenOfBody>\n" +
                    "<AlarmText>\n" +
                    "{\"PVFlag\":\"PNF\",\"addInfo\":\"\",\"alarmId\":\"22016093\",\"alarmSeq\":32132474,\"alarmStatus\":1,\"alarmTitle\":\"中继群的不可用电路数超过阈值\",\"alarmType\":\"信令系统\",\"eventTime\":\"2021-07-02 03:08:03\",\"locationInfo\":\"中继群名称=PRA-DAZHONG10-4(659), 设备类型=PRA, 严重级别=一级告警, 安装电路数=30, 故障电路数=30, 闭塞电路数=0, 未知电路数=0, 不可用电路百分比=100%, 局向名称=PRA-DAZHONG10(139)\",\"neName\":\"SHTDMMGCF1\",\"neType\":\"MGCF\",\"neUID\":\"3101HWHX1MNE3221287051\",\"objectName\":\"SHTDMMGCF1\",\"objectType\":\"MgcfFunction\",\"objectUID\":\"3101HWHX1MGC3223289859\",\"origSeverity\":2,\"specificProblem\":\"14\",\"specificProblemID\":\"2371\"}\n" +
                    "</AlarmText>\n" +
                    "</Root>\n";
            String division = "中继群名称=";
            String substring = xml.substring(xml.indexOf(division));
            res = substring.substring(division.length(),
                    substring.indexOf("-", substring.indexOf("-") + 1));
            System.out.println(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
